<template>
    <div class="reset-password-container">
        <el-card class="password-card">
            <template #header>
                <div class="card-header">
                    <h2>重置密码</h2>
                </div>
            </template>
            
            <el-form :model="passwordForm" :rules="rules" ref="passwordFormRef" label-width="100px">
                <el-form-item label="原密码" prop="oldPassword">
                    <el-input v-model="passwordForm.oldPassword" type="password" show-password></el-input>
                </el-form-item>
                
                <el-form-item label="新密码" prop="newPassword">
                    <el-input v-model="passwordForm.newPassword" type="password" show-password></el-input>
                </el-form-item>
                
                <el-form-item label="确认新密码" prop="confirmPassword">
                    <el-input v-model="passwordForm.confirmPassword" type="password" show-password></el-input>
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="submitForm" :loading="loading">确认修改</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { resetPasswordService } from '@/api/user.js'
import { useTokenStore } from '@/stores/token'
import { useRouter } from 'vue-router'

const tokenStore = useTokenStore()
const router = useRouter()
const loading = ref(false)
const passwordFormRef = ref(null)

// 表单数据
const passwordForm = reactive({
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
})

// 验证确认密码是否与新密码一致
const validateConfirmPassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次输入密码'))
    } else if (value !== passwordForm.newPassword) {
        callback(new Error('两次输入密码不一致'))
    } else {
        callback()
    }
}

// 表单验证规则
const rules = {
    oldPassword: [
        { required: true, message: '请输入原密码', trigger: 'blur' },
        { min: 5, max: 16, message: '密码长度为5-16个字符', trigger: 'blur' }
    ],
    newPassword: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { min: 5, max: 16, message: '密码长度为5-16个字符', trigger: 'blur' }
    ],
    confirmPassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { validator: validateConfirmPassword, trigger: 'blur' }
    ]
}

// 提交表单
const submitForm = () => {
    passwordFormRef.value.validate(async (valid) => {
        if (valid) {
            loading.value = true
            try {
                const response = await resetPasswordService({
                    old_pwd: passwordForm.oldPassword,
                    new_pwd: passwordForm.newPassword,
                    re_pwd: passwordForm.confirmPassword
                })
                
                ElMessage.success('密码修改成功，请重新登录')
                
                // 清除token并跳转到登录页
                tokenStore.removeToken()
                router.push('/login')
            } catch (error) {
                ElMessage.error(error.message || '密码修改失败，请稍后再试')
            } finally {
                loading.value = false
            }
        }
    })
}

// 重置表单
const resetForm = () => {
    passwordFormRef.value.resetFields()
}
</script>

<style scoped>
.reset-password-container {
    display: flex;
    justify-content: center;
    padding: 20px;
}

.password-card {
    width: 500px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>